﻿using BDQN.Dtos;
using BDQN.IBLL;
using BDQN.IDAL;
using BDQN.Entities;
namespace BDQN.BLL
{

    public class RolesBll : IRolesBll
    {
        //1. 注入DAL层对象
        private IRolesDal _dal;
        public RolesBll(IRolesDal dal)
        {
            _dal = dal;
        }


        public async Task<int> AddRolesAsync(string title)
        {
            return await _dal.AddAsync(new Roles
            {
                Title = title
            });
        }

        public async Task<int> DeleteRolesAsync(int id)
        {
            //1. 查询
            var data = await _dal.QueryAsync(id);
            if (data == null) // 判断查询出来的数据是否为空，如果为空，返回-1提示不存在
                return -1;
            //return await _dal.RemoveAsync(data); // 如果不是空的调用删除方法删除该对象
            //我们现在因为有外键关系，并能真正的执行删除，所以要求我们实现的是软删除
            data.DeleteId = 0;
            data.UpdateTime = DateTime.Now;
            return await _dal.EditAsync(data);
        }

        public async Task<int> EditRolesAsync(int id, string title)
        {
            var data= await _dal.QueryAsync(id); // 查询当前内容是否存在
            if (data == null)
                return -1;
            data.Title = title;
            data.UpdateTime = DateTime.Now;
            return await _dal.EditAsync(data);
        }

        public async Task<List<RolesDto>> GetRolesAsync()
        {
            var data = await _dal.QueryAsync();
            return data.Any() ? 
                     data.Select(r => new RolesDto 
                     {
                        Id = r.Id,
                        Title = r.Title,
                        UpdateTime = r.UpdateTime,
                        CreateTime = r.CreateTime
                     }).ToList()
                     : new List<RolesDto>();
        }

        public async Task<List<RolesDto>> GetRolesAsync(string title)
        {
            var data = await _dal.QueryAsync(r => r.DeleteId == 1 && r.Title.Contains(title));
            return data.Any() ?
                     data.Select(r => new RolesDto 
                     {
                         Id = r.Id,
                         Title = r.Title,
                         UpdateTime = r.UpdateTime,
                         CreateTime = r.CreateTime
                     }).ToList()
                     : new List<RolesDto>();
        }

        public async Task<RolesDto> GetRolesAsync(int id)
        {
            var data = await _dal.QueryAsync(id);
            return data == null ? new RolesDto() : new RolesDto 
            {
                Id = data.Id,
                Title = data.Title,
                UpdateTime = data.UpdateTime,
                CreateTime = data.CreateTime
            };
        }

        public string GetRolesTitle(int id)
        {
            var data = _dal.Query(id);
            return data == null ? "" : data.Title;
        }

        public async Task<int> DeleteRolesListAsync(params int[] idList)
        {
            var list = await GetRolesByIdListAsync(idList);

            foreach (Roles data in list)
            {
                data.DeleteId = 0;
                data.UpdateTime = DateTime.Now;
                await _dal.EditAsync(data, false);
            }
            return await _dal.SaveDataAsync();
        }

        public async Task<List<Roles>> GetRolesByIdListAsync(params int[] idList)
        {
           return  await _dal.QueryAsync(r => r.DeleteId == 1 && idList.Contains(r.Id)); 
        }
    }
}
